(function(){
	angular.module("webApp").controller("ShoppingcartCtrl",ShoppingcartCtrl);
	ShoppingcartCtrl.$inject=['$scope','dataService','$rootScope','$location','sessionService','$timeout','helper'];
	function ShoppingcartCtrl($scope,dataService,$rootScope,$location,sessionService,$timeout,helper){
		$scope.cartAction=function(){
			$scope.buyChecked={};
			$scope.cartTotal=0;
			$scope.cartGetList();
			$scope.buyCheckedDisabled=true;
		}
		$scope.cartGetList=function(){
			// $scope.cartEdit="load";
			$scope.shops={};
			dataService.cartGet().then(function(carts){
				async.map(carts.cart_items,function(cart,callback){
					dataService.itemOne(cart.item_id).then(function(result){
						if(result.Msg){
							helper.myAlert("error:"+result.Msg);
						}else{
						  cart.item=result;
							
							_.map(cart.item.skus,function(sku){
								if(sku.sku_id==cart.sku_id){
								  	sku.sku_checked=true;
								}
								var skus_properties_array=sku.sku_properties.split(";");
								sku.skus_propertie_name="";
								_.map(skus_properties_array,function(skus_propertie){
									if($.trim(skus_propertie)){
										var sku_key= skus_propertie.split(":");
										sku.skus_propertie_name+=sku_key[2]+":"+sku_key[3]+";";
									}
								});
							});
						}
					});
					dataService.storeGet({query:{store_id:cart.shop_id}}).then(function(result){
						
					
					if(!$scope.shops[cart.shop_id]){
						$scope.shops[cart.shop_id]={};
						$scope.shops[cart.shop_id].carts=[];
						$scope.shops[cart.shop_id].shop={};
						$scope.shops[cart.shop_id].cartEdit="load";

					}

					$scope.shops[cart.shop_id].carts.push(cart);
					if(result.Msg){
							helper.myAlert("error:"+result.Msg);
					}else{
							$scope.shops[cart.shop_id].shop=result.store[0];
					}
					});
					callback(null,null);
				},function(e,r){
					// $scope.carts=carts;
				})
				
			});
			$scope.$watch("shops",function(news,old){
				$scope.cartTotal=0;
				_.map(news,function(shop){
					_.map(shop.carts,function(cart){
						if(cart.deleteCheckbox){
							$scope.cartTotal+=cart.quantity*cart.price;
						}
					});
				});
			},true)
		}
		$scope.Jumpshop=function(shop_id,shop){
			if(shop.cartEdit=="load"){
				$location.url("shop/list?shop_id="+shop_id);
			}
		}
		$scope.isShops=function(){
			if(_.size($scope.shops)>0){
				return true;
			}else{
				return false;
			}
		}
		$scope.cartDelete=function(shop,cart,index){
			dataService.cartDelete(cart.cart_item_id).then(function(r){
		  			if(r.Msg){
		  				helper.myAlert("error:"+r.Msg);
		  			}else{
		  				// $scope.cartGetList();
		  			 return	shop.carts.splice(index,1);
		  			}
		  	});
		}
		$scope.buyOrderAdd=function(cart,shop){
			// $scope.buyChecked[cart.sku_id]=cart.quantity;
			// $scope.buyChecked[cart.cart_id]=cart.shop_id;
			if(_.size($scope.buyChecked)==0){
				if(cart.deleteCheckbox){
					$scope.buyCheckedDisabled=false;
				    $scope.buyChecked[cart.cart_item_id]=cart.shop_id;
				}
				
			}else{
				async.map($scope.buyChecked,function(shop_id,callback){

						if(shop_id==cart.shop_id){
							$scope.buyCheckedDisabled=false;

						}else if(shop_id!=cart.shop_id){
							$scope.buyCheckedDisabled=true;
						}
					callback(null,null);
				},function(e,r){
					if(cart.deleteCheckbox){
						if(!$scope.buyCheckedDisabled){
								$scope.buyChecked[cart.cart_item_id]=cart.shop_id;

						}else{
								helper.myAlert("只能结算一个店铺内的商品");
								// $scope.buyCheckedDisabled=false;
								shop.allchecked=false;
								cart.deleteCheckbox=false;

						}
					}else if((!cart.deleteCheckbox) && (!shop.allchecked)){
						if(!$scope.buyCheckedDisabled){
							delete	$scope.buyChecked[cart.cart_item_id];
							if(_.size($scope.buyChecked)==0){
								// $scope.buyCheckedDisabled=true;
								shop.allchecked=false;
							}
						}else{
								helper.myAlert("只能结算一个店铺内的商品");
								// $scope.buyCheckedDisabled=false;
								cart.deleteCheckbox=false;
								shop.allchecked=false;
						}
					}
					
				});
			}
		}
	


		$scope.updateListEditStatu=function(shop){
			console.log("shop",shop);
			if(shop.cartEdit=="load"){
				shop.cartEdit="edit";
			}else{
				shop.cartEdit="load";
			}
		}
		$scope.herfItemInfo=function(shop,item){
			if(shop.cartEdit=='load'){
				$location.url("item/info?item_id="+item.item_id);
			}
		}
		$scope.checkshopAllItem=function(shop){
			if(shop.allchecked){

				_.map(shop.carts,function(cart){
					if(cart.deleteCheckbox){
					}else{
						cart.deleteCheckbox=true;
					}
					$scope.buyOrderAdd(cart,shop);
				})
			}else{
				_.map(shop.carts,function(cart){
					cart.deleteCheckbox=false;
					$scope.buyOrderAdd(cart,shop);
				})
			}

		}
		$scope.cartnumTab=function(cart,operator){
			var quantity ;
			if(operator=='-'){
				quantity=cart.quantity-=1;
				var data={cart_item_id:cart.cart_item_id,quantity:quantity};
		  		dataService.cartupdatenum(data).then(function(r){
		  			if(r.Msg){
		  				cart.quantity+=1;
		  				helper.myAlert("error:"+r.Msg);
		  			}else{
		  				cart.quantity=quantity;
		  			}
		  		});
			}else if(operator=='+'){
				quantity=cart.quantity+=1;
				var data={cart_item_id:cart.cart_item_id,quantity:quantity};
		  		dataService.cartupdatenum(data).then(function(r){
		  			if(r.Msg){
		  				cart.quantity-=1;
		  				helper.myAlert("error:"+r.Msg);
		  			}else{
		  				cart.quantity=quantity;
		  			}
		  		});
			}
		}
	}
})();